import { BaseService } from '@app/db/base/base.service';
import { Device } from '@app/db/entities/Device';
import { PricingFastCharge } from '@app/db/entities/PricingFastCharge';
import { PricingSlowCharge } from '@app/db/entities/PricingSlowCharge';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { AliIotService } from 'apps/ali-iot/src/ali-iot.service';
import { FindOptionsSelect, Repository } from 'typeorm';
import { PrivateDeviceService } from '../private-device/private-device.service';
import { PrivateDevice } from '@app/db/entities/PrivateDevice';
import { ChargingStation } from '@app/db/entities/ChargingStation';

@Injectable()
export class DeviceService extends BaseService<Device>{
    constructor(
        @InjectRepository(Device) protected repository:Repository<Device>,
        @InjectRepository(PrivateDevice) protected privateDevice:Repository<PrivateDevice>,
        @InjectRepository(PricingSlowCharge) protected pricingSlowCharge:Repository<PricingSlowCharge>,
        @InjectRepository(PricingFastCharge) protected pricingFastCharge:Repository<PricingFastCharge>,
        @InjectRepository(ChargingStation) protected chargingStation:Repository<ChargingStation>,
        private readonly aliIot:AliIotService,


    ) {
        super()
        this.primaryKey = 'id'
    }


    async queryPrivateAuth(deviceId:number, userId:number){
        const device = await this.privateDevice.findOne({
            where : {id : deviceId},
            select : ['owners']
        })

        if(!device || !device.owners.includes(userId)) return false
        return true
    }


    queryDeviceById(id:number, fields:(keyof Device)[]) {
        return this.findOne({id : id}, fields)
    }

    queryDeviceByIds(ids:number[], fields:FindOptionsSelect<Device>) {
        return this.find(ids.map(id => ({id})), fields)
    }

    baseInfo(id:number) {
        return this.findOne({id : id}, ['address', 'chargeType', 'portCount', 'portStatus', 'private', 'stationId', 'connectStatus'])
    }

    slowPricing(id:number) {
        return this.pricingSlowCharge.findOne({
            where : {id},
            select : ['price', 'service', 'hourKwh']
        })
    }

    async station(stationId:number, chargeType:number) {
        let res:{station?:ChargingStation, pricing?:PricingFastCharge|PricingSlowCharge} = {

        }
        const station = await this.chargingStation.findOne({
            where : {id : stationId},
            select : ['fastPricing', 'slowPricing', 'address', 'longitude', 'latitude', 'name']
        })

        if(!station) return res

        if(chargeType == 1) {
            if(station.fastPricing > 0) {
                res.pricing = await this.pricingFastCharge.findOne({
                    where : {id : station.fastPricing},
                    select : ['rules', 'pay']
                })
            }
        }else {
            if(station.slowPricing > 0) {
                res.pricing = await this.pricingSlowCharge.findOne({
                    where : {id : station.slowPricing},
                    select : ['price', 'service', 'hourKwh']
                })
            }
        }

        res.station = station

        return res
    }

    queryDeviceChargingStatus(deviceId:number, aliProduct:string) {
        return this.aliIot.pubToDevice(deviceId, aliProduct, {type : 9})
    }

}
